/*
 * Copyright, Aspect Security, Inc.
 *
 * This file is part of JavaSnoop.
 *
 * JavaSnoop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JavaSnoop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JavaSnoop.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aspect.snoop.ui.tamper.common;

import com.aspect.snoop.util.ReflectionUtil;

public class AddItemView extends javax.swing.JDialog {

    /** Creates new form AddItemView */

    Object toReturn;

    private void initialize() {
        initComponents();
        toReturn = null;
    }

    public AddItemView(javax.swing.JDialog parent, boolean modal, Class itemClass) {
        super(parent, modal);
        initialize();

        for(int i=0;i<lstDataType.getItemCount();i++) {
            String row = (String)lstDataType.getItemAt(i);
            String cls = ReflectionUtil.getSimpleClassName(itemClass.getName());
            if ( row.equals(cls) ) {
                lstDataType.setSelectedIndex(i);
                lstDataType.setEnabled(false);
                i = lstDataType.getItemCount(); //terminate loop
            }
        }
    }

    public AddItemView(javax.swing.JDialog parent, boolean modal) {
        super(parent, modal);
        initialize();
    }

    public Object getNewItem() {
        return toReturn;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lstDataType = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtValue = new javax.swing.JTextField();
        btnAddItem = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.aspect.snoop.JavaSnoop.class).getContext().getResourceMap(AddItemView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lstDataType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "String", "Integer", "Boolean", "Long", "Double", "Float", "Byte" }));
        lstDataType.setFocusable(false);
        lstDataType.setName("lstDataType"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtValue.setName("txtValue"); // NOI18N
        txtValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValueActionPerformed(evt);
            }
        });

        btnAddItem.setFont(resourceMap.getFont("btnAddItem.font")); // NOI18N
        btnAddItem.setText(resourceMap.getString("btnAddItem.text")); // NOI18N
        btnAddItem.setName("btnAddItem"); // NOI18N
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lstDataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtValue, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                            .addComponent(jLabel3)))
                    .addComponent(btnAddItem, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lstDataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddItem)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValueActionPerformed
        // user pressed enter -> click the save button
        btnAddItemActionPerformed(evt);
}//GEN-LAST:event_txtValueActionPerformed

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        String dataType = (String)lstDataType.getSelectedItem();

        try {
            if ( "String".equals(dataType) ) {
                toReturn = txtValue.getText();
            } else if ( "Integer".equals(dataType) ) {
                toReturn = Integer.parseInt(txtValue.getText());
            } else if ( "Float".equals(dataType) ) {
                toReturn = Float.parseFloat(txtValue.getText());
            } else if ( "Boolean".equals(dataType) ) {
                if ( ! "true".equalsIgnoreCase(txtValue.getText()) && ! "false".equalsIgnoreCase(txtValue.getText()) ) {
                    throw new Exception();
                }
                toReturn = Boolean.parseBoolean(txtValue.getText());
            } else if ( "Double".equals(dataType) ) {
                toReturn = Double.parseDouble(txtValue.getText());
            } else if ( "Long".equals(dataType) ) {
                toReturn = Long.parseLong(txtValue.getText());
            } else if ( "Byte".equals(dataType) ) {
                toReturn = Byte.parseByte(txtValue.getText());
            }

            dispose();

        } catch (Exception e) {
            toReturn = null;
        }
}//GEN-LAST:event_btnAddItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AddItemView dialog = new AddItemView(new javax.swing.JDialog(), true, Integer.class);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox lstDataType;
    private javax.swing.JTextField txtValue;
    // End of variables declaration//GEN-END:variables
}
